package com.fefa.coins.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.fefa.coins.db.Consultas;

public class Divisa extends Consultas{
	private long id = 0;
	private String nombre = null;
	private String siglas = null;
	private String fraccion = null;
	private int multiplo = 1;
	
	public static int EURO = 1;

	private final static String GET_DIVISA_SQL = "SELECT `divisas`.`id`, `divisas`.`divisa`, `divisas`.`siglas`" +
	", `divisas`.`fraccion`, `divisas`.`multiplo` FROM `divisas` ";

	private final static String GET_DIVISA_PAIS_SQL = "SELECT `divisas`.`id`, `divisas`.`divisa`, `divisas`.`siglas`" +
	", `divisas`.`fraccion`, `divisas`.`multiplo` FROM `divisas`, `pais_divisa` WHERE (`divisas`.`id` = `pais_divisa`.`divisa`) ";

	public Long insert(Connection conn) {
		// Validaciones para insertar
		// Insertar
		// Obtiene el id y lo guarda
		return null;
	}

	public boolean delete(Connection conn) {

		return false;
	}

	public boolean update(Connection conn) {
		return false;
	}

	public static Divisa readDivisa(Connection conn, Long _id) throws SQLException {
		Divisa _divisa = new Divisa(); 
		PreparedStatement stmt=null;
		ResultSet rs = null;

		stmt=conn.prepareStatement(GET_DIVISA_SQL + "WHERE id=?");
		stmt.setLong(1, _id.longValue());
		rs = stmt.executeQuery();
		if (rs.next()) 
			_divisa.loadDivisaFromResultSet(rs);
		else 
			_divisa = null;

		if (rs != null) rs.close();
		if (stmt != null) stmt.close(); 

		return _divisa;
	}

	public static ArrayList<Divisa> searchDivisas(Connection conn, String where, String sort) throws SQLException {
		String query = GET_DIVISA_SQL;
		if ((where != null) && (where.trim().length()>0))
			query +="WHERE " + where;

		return search(conn, query, sort);
	}

	public static ArrayList<Divisa> searchDivisasByPais(Connection conn, String where, String sort) throws SQLException {
		String query = GET_DIVISA_PAIS_SQL;
		if ((where != null) && (where.trim().length()>0)) 
			query += " AND " + where;

		return search(conn, query, sort);
	}

	private static ArrayList<Divisa> search(Connection conn, String sql, String sort) throws SQLException {
		String query = sql;

		if ((sort != null) && (sort.trim().length()>0)) {
			query +="ORDER BY " + sort + " ";
		}
		// Creamos el Statement y el ResultSet.
		Statement stm = conn.createStatement();
		ResultSet rs = stm.executeQuery(query);

		// Procesamos el ResultSet.
		ArrayList<Divisa> lDivisas = null;
		while (rs.next()) {
			Divisa _divisa = new Divisa();
			_divisa.loadDivisaFromResultSet(rs);
			if (null == lDivisas)
				lDivisas = new ArrayList<Divisa>();
			lDivisas.add(_divisa);
		}
		rs.close();
		stm.close();
		return lDivisas;
	}

	private void loadDivisaFromResultSet(ResultSet rs) throws SQLException {
		this.id = rs.getLong(1);
		this.nombre = rs.getString(2);
		this.siglas = rs.getString(3);
		this.fraccion = rs.getString(4);
		this.multiplo = rs.getInt(5);
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getSiglas() {
		return siglas;
	}

	public void setSiglas(String siglas) {
		this.siglas = siglas;
	}

	public String getFraccion() {
		return fraccion;
	}

	public void setFraccion(String fraccion) {
		this.fraccion = fraccion;
	}

	public int getMultiplo() {
		return multiplo;
	}

	public void setMultiplo(int multiplo) {
		this.multiplo = multiplo;
	}

}
